/*
 *      BranchByFalse.java
 *
 *      Copyright 2008 Pablo Nicolas Diaz Bilotto <pablonicolas.diaz@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
package ilanguage.iltokens.operations.control;

import ilanguage.iltokens.IL_SSA;
import ilanguage.iltokens.operations.OperationIL_SSA;
import ilanguage.iltokens.operations.Operations;
import ilanguage.iltokens.operations.TwoOperandIL_SSA;
import ilanguage.iltokens.operations.logical.Equal;
import ilanguage.iltokens.operations.logical.Greater;
import ilanguage.iltokens.operations.logical.GreaterEqual;
import ilanguage.iltokens.operations.logical.Less;
import ilanguage.iltokens.operations.logical.LessEqual;
import ilanguage.iltokens.operations.logical.NotEqual;


/**
 * The Class BranchByFalse.
 */
public class BranchByFalse extends TwoOperandIL_SSA {


	/** The Token to decide. */
	private IL_SSA TokenToDecide;

	/**
	 * Instantiates a new branch by false.
	 *
	 * @param firstToken the first token
	 * @param secToken the sec token
	 */
	public BranchByFalse(IL_SSA firstToken, IL_SSA secToken) {
		super(firstToken,secToken);
		this.TokenToDecide = secToken;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ilanguage.iltokens.operations.control.Branch#GenerateCodeMachine()
	 */
	@Override
	public String GenerateCode()
	{
		IL_SSA firstArg = this.getFirstArg();
		if (firstArg!= null){
		String sec= firstArg.getSequence();
		String codigo=  "\t; Comienzo de Branch por falso\n";
		String instrSalto="";
		if (TokenToDecide instanceof Equal)
			instrSalto="jne";
		else if (TokenToDecide instanceof Greater)
			instrSalto="jle";
		else if (TokenToDecide instanceof Less)
			instrSalto="jge";
		else if (TokenToDecide instanceof LessEqual)
			instrSalto="jg";
		else if (TokenToDecide instanceof GreaterEqual)
			instrSalto="jl";
		else if (TokenToDecide instanceof NotEqual)
			instrSalto="je";
		codigo+=  "\t"+instrSalto+" "+sec+"\n";
		codigo+=  "\t; Fin de branch por falso\n\n";
		return codigo;
		}
		return "";
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see ilanguage.iltokens.operations.control.Branch#getOperationName()
	 */
	@Override
	public String getOperationName() {
		return Operations.BF.toString();
	}

	/**
	 * Gets the token to decide.
	 *
	 * @return the token to decide
	 */
	public IL_SSA getTokenToDecide() {
		return TokenToDecide;
	}


	/**
	 * Sets the token to decide.
	 *
	 * @param tokenToDecide the new token to decide
	 */
	public void setTokenToDecide(IL_SSA tokenToDecide) {
		TokenToDecide = tokenToDecide;
	}

	public boolean isDiscartable() {
		return OperationIL_SSA.discartable(getSecondArg());
	}
}